package com.controller;

import com.entity.Film;
import com.service.FilmService;
import com.service.IndexService;
import com.service.PlayService;
import com.vo.FilmDetailVO;
import com.vo.FilmVO;
import com.vo.PlayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class FilmController {
    @Autowired
    private FilmService filmService;
    @Autowired
    private PlayService playService;
    @Autowired
    private IndexService indexService;

    @RequestMapping("/filmList")
    @ResponseBody
    public List<FilmDetailVO> selectAll(Model model){
        return filmService.selectAll();
    }

    @RequestMapping("/home")
    public String home(Model model){
        model.addAttribute("filmListData",filmService.selectAll());
        model.addAttribute("filmListSoon",indexService.selectSoon());
        return "home";
    }

    @RequestMapping("filmInfo")
    public String filmDetail(Model model,String filmId){
        model.addAttribute("film",filmService.filmDetail(filmId));
        model.addAttribute("playVos", playService.findPalyById(filmId));
        return "filmDetail";
    }

    @RequestMapping("filmSoon")
    public String filmDetailSoon(Model model,String filmId){
        model.addAttribute("film",indexService.filmDetail(filmId));
        model.addAttribute("playVos", playService.findPalyById(filmId));
        return "filmDetail";
    }

    @RequestMapping("/filmshow")
    public String filmshow(){
        return "filmUpdate";
    }

    @RequestMapping("/filmAll")
    @ResponseBody
    public Map<String,Object> filmAll(@RequestParam int page, @RequestParam int limit){
        List<FilmVO> filmVOS = filmService.filmAll(page*limit-limit, limit);

        Map<String, Object> map = new LinkedHashMap<>();
        map.put("code", 0);
        map.put("msg", "数据错误");
        map.put("count", filmService.count());
        map.put("data", filmVOS);
        return  map;
    }

    @RequestMapping("/film")
    public String film(){return "filmUpdate";}

    @PostMapping("/filmadd")
    public String filmadd(@ModelAttribute(value = "film") FilmVO film, MultipartFile img, HttpServletRequest request, Model model){
        System.out.println(film);
        String msg ="上架失败";
        if (checkFile(img.getOriginalFilename())){
            String uuid = "p"+ UUID.randomUUID().toString().replaceAll("-", "").substring(0,8);
            if (filmService.filmById(uuid)){
                String uploadPath = request.getSession().getServletContext().getRealPath("/imgs/");
                try {
                    msg ="上架成功";
                    img.transferTo(new File(uploadPath,uuid+".jpg"));
                    film.setFilmId(uuid);
                    film.setImgPath("/imgs/"+uuid+".jpg");
                    if(filmService.insert(film)!=1){
                        msg ="上架失败";
                    }
                } catch (IOException e) {
                    msg ="上架失败";
                }
            }else{
                msg ="上架失败,ID分配重复";
            }
        }else {
            msg = "文件类型不匹配";
        }

        model.addAttribute("msg","layer.open({\n" +
                "                type: 0,\n" +
                "                content: '"+msg+"',\n" +
                "                offset:'t'\n" +
                "            });");
        return "filmUpdate";
    }

    /**
     * 判断是否为允许的上传文件类型,true表示允许
     */
    private boolean checkFile(String fileName) {
        //设置允许上传文件类型
        String suffixList = "jpg,png,ico,bmp,jpeg";
        // 获取文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".")
                + 1, fileName.length());
        if (suffixList.contains(suffix.trim().toLowerCase())) {
            return true;
        }
        return false;
    }

}
